package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjAboutUs;
import com.moji.entity.dto.QueryAboutUsDto;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.service.MjAboutUsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 关于我们表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-07-20
 */
@Service
public class MjAboutUsServiceImpl extends ServiceImpl<MjAboutUsMapper, MjAboutUs> implements MjAboutUsService {

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjAboutUs> queryByPage(QueryAboutUsDto dto) {
        LambdaQueryWrapper<MjAboutUs> wrapper = new LambdaQueryWrapper<>();
        //标题
        wrapper.like(ObjectUtil.isNotEmpty(dto.getTitle()),MjAboutUs::getTitle
                ,dto.getTitle());
        //创建时间倒叙
        wrapper.orderByDesc(MjAboutUs::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjAboutUs> mjAboutUsList = list(wrapper);
        PageInfo<MjAboutUs> info = new PageInfo<>(mjAboutUsList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean add(MjAboutUs mjAboutUs) {
        //编码自动生成
        String code = idGenerator.idGenerator("GYWM");
        mjAboutUs.setCode(code);
        mjAboutUs.setCreateId(MoJiCommonUtils.getUserCode());
        mjAboutUs.setUpdateId(MoJiCommonUtils.getUserCode());
        mjAboutUs.setCreateTime(new Date());
        mjAboutUs.setUpdateTime(new Date());
        return this.save(mjAboutUs);
    }

    @Override
    public boolean edit(MjAboutUs mjAboutUs) {
        MjAboutUs mjAboutUsOld=this.getById(mjAboutUs.getId());
        if(mjAboutUsOld ==null){
            throw new RuntimeException("数据不存在,请核查!");
        }
        BeanUtils.copyProperties(mjAboutUs,mjAboutUsOld);
        mjAboutUs.setUpdateId(MoJiCommonUtils.getUserCode());
        mjAboutUs.setUpdateTime(new Date());
        return this.updateById(mjAboutUs);
    }

    @Override
    public MjAboutUs detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.getById(id);
    }
}
